package isssr.entity.src;

import java.io.Serializable;
import java.util.HashMap;

public class Role  implements Serializable{

	private String title;
	private String description;
	private Integer level;
	
	public Role(RoleDTO data)
	{
		this.title = ( data.getTitle() != null ? new String(data.getTitle()) : null);
		this.description = ( data.getDescription() != null ? new String(data.getDescription()) : null);
		this.level = ( data.getLevel() != null ? new Integer(data.getLevel()) : null);
	}
	
	// this constructor is for Hibernate
	protected Role() {}

	public Role(String title, Integer level) 
	{
		this.title = new String(title);
		this.level = new Integer(level);
	}

	private String getTitle() {
		return title;
	}

	private void setTitle(String title) {
		this.title = title;
	}

	private String getDescription() {
		return description;
	}

	private void setDescription(String description) {
		this.description = description;
	}

	private Integer getLevel() {
		return level;
	}

	private void setLevel(Integer level) {
		this.level = level;
	}

	public RoleDTO getDTO()
	{
		RoleDTO data = new RoleDTO(this.title, this.description, this.level);
		
		return data;
	}
	
	public void update(RoleDTO data)
	{
		this.title = new String(data.getTitle());
		this.description = new String(data.getDescription());
		this.level = new Integer(data.getLevel());
	}
	
	public Integer compare(Role role)
	{
		if(this.level < role.level)
			return -1;
		if(this.level > role.level)
			return 1;
		
		return 0;
	}
	
	@Override
	public boolean equals(Object o)
	{
		if(o == null)
			return false;
		if( ! (o instanceof Role) )
			return false;
		
		Role otherRole = (Role) o;
		
		if( (this.title == null && otherRole.title!=null))
			return false;
		if( otherRole.title!=null && !(this.title.equals(otherRole.title)) )
			return false;
		
		if( (this.description == null && otherRole.description!=null))
			return false;
		if( otherRole.description!=null && !(this.description.equals(otherRole.description)) )
			return false;
		
		if( !this.level.equals(otherRole.level))
			return false;
		
		return true;
	}
	
	
	@Override
	public String toString()
	{
		String output = "Role[";

		output = output.concat( title != null ? title : "");
		output = output.concat(", ");
		output = output.concat( level != null ? level.toString() : "");
		output = output.concat(", ");
		output = output.concat( description != null ? "" + description.hashCode() : "");
		
		return output;
	}
}
